#!/bin/sh
# get title and artist from cmus(1) if playing otherwise 'paused'

query=$(cmus-remote -Q \
		2>/dev/null |
		head -n10
)

# quit if cmus is not running
[ -z "${query}" ] && exit 1

# get title, artist and status
while IFS=' ' read -r v1 v2 v3; do
	case "${v1}_${v2}" in
	tag_title)
		title="${v3}";;
	tag_artist)
		artist="${v3}";;
	status_*)
		status="${v2}";;
	esac
done << _EOF_
$(printf '%s\n' "${query}")
_EOF_

# append artist only if not empty
[ -n "${artist}" ] && artist=" - ${artist}"

# cut title and artist if too long
[ $(($(printf '%s' "${title}" | wc -m))) -gt 32 ] && title=$(printf '%s' "${title}" | cut -c-32)...
[ $(($(printf '%s' "${artist}" | wc -m))) -gt 32 ] && artist=$(printf '%s' "${artist}" | cut -c-20)...

[ "${status}" = playing ] &&
	printf '%s%s\n' "${title}" "${artist}" ||
	printf 'paused\n'
